package com.wantong.jianpu.customviews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;
import com.wantong.jianpu.R;
import com.wantong.jianpu.adapters.DetailImageAdapter;
import com.wantong.jianpu.adapters.DetailRecordsAdapter;
import com.wantong.jianpu.beans.FaultDetailBean;
import com.wantong.jianpu.utils.Constants;

/**
 * @author PACY
 * @date 2022/4/25
 * description: 维修记录  已修复待确认
 */
public class FaultRepairGoneView extends LinearLayout {



    private RecyclerView image_rv;
    public DetailRecordsAdapter mAdapter;
    private GridLayoutManager layoutManager;


    private FaultDetailBean.RecordsListDTO recordsBean;

    private TextView label_tv;//提示
    private LinearLayout content_lin;//详情数据UI

    private CommTextView state_ctv;//维修状态
    private CommTextView nature_ctv;//维修性质
    private CommTextView type_ctv;//维修方式
    private CommTextView user_ctv;//维修人员
    private CommTextView helpUser_ctv;//协助人员
    private CommTextView time_ctv;//时间
    private CommTextView grade_ctv;//维修等级
    private CommTextView analysis_ctv;//故障分析
    private CommTextView measures_ctv;//处理措施
    private CommTextView price_ctv;//维修金额


    public FaultRepairGoneView(Context context) {
        this(context,null);
    }

    public FaultRepairGoneView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.fault_repairgone_view, this, true);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    /**
     * 初始化View
     */
    public void initView(){
        label_tv=findViewById(R.id.label_tv);
        content_lin=findViewById(R.id.content_lin);
        user_ctv=findViewById(R.id.user_ctv);
        helpUser_ctv=findViewById(R.id.helpUser_ctv);
        time_ctv=findViewById(R.id.time_ctv);
        state_ctv=findViewById(R.id.state_ctv);
        nature_ctv=findViewById(R.id.nature_ctv);
        type_ctv=findViewById(R.id.type_ctv);
        grade_ctv=findViewById(R.id.grade_ctv);
        analysis_ctv=findViewById(R.id.analysis_ctv);
        measures_ctv=findViewById(R.id.measures_ctv);
        price_ctv=findViewById(R.id.price_ctv);

        image_rv=findViewById(R.id.image_rv);
        mAdapter=new DetailRecordsAdapter();
        layoutManager=new GridLayoutManager(getContext(),4);
        image_rv.setLayoutManager(layoutManager);
        image_rv.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            new XPopup.Builder(getContext()).asImageViewer((ImageView) mAdapter.getViewByPosition(position,R.id.image_iv), mAdapter.getItem(position).getUrl(), new SmartGlideImageLoader()).show();
        });

        initData();
    }
    public void setData(FaultDetailBean.RecordsListDTO recordsBean){
        this.recordsBean=recordsBean;
        initView();
    }
    /**
     * 初始化数据
     */
    public void initData(){
        if(recordsBean!=null){
            if(recordsBean.isIfLast()){
                label_tv.setVisibility(VISIBLE);
                label_tv.setText(recordsBean.getLastText());
                content_lin.setVisibility(GONE);
            }else{
                label_tv.setVisibility(GONE);
                content_lin.setVisibility(VISIBLE);
                state_ctv.setContent("已修复,待确认");
                if(recordsBean.getNature()==1){
                    nature_ctv.setContent("一般");
                }else{
                    nature_ctv.setContent("抢险");
                }
                if(recordsBean.getRepairType()==1){
                    nature_ctv.setContent("现场维修");
                }else{
                    nature_ctv.setContent("电话指导");
                }
                user_ctv.setContent(recordsBean.getCreateUserName());
                helpUser_ctv.setContent(recordsBean.getHelpUsersName());
                time_ctv.setContent(recordsBean.getCreateTime());
                mAdapter.setNewInstance(recordsBean.getRepairFiles());
                grade_ctv.setContent(recordsBean.getMaintainLevelName());
                analysis_ctv.setContent(recordsBean.getConfMeasuresName());
                measures_ctv.setContent(recordsBean.getMeasures());
                price_ctv.setContent(recordsBean.getRepairPrice()+"");
            }
        }
    }
}
